package com.ruoyi.StarStudio.Utils;

import org.apache.commons.codec.binary.Hex;
import org.apache.tomcat.util.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

@SuppressWarnings("unused")
public class DESedeUtils {

    /**
     * 密钥算法
     */
    private static final String KEY_ALGORITHM = "DESede";

    /**
     * 加密/解密算法 / 工作模式 / 填充方式
     * Java 6支持PKCS5Padding填充方式
     * Bouncy Castle支持PKCS7Padding填充方式
     */
    private static final String CIPHER_ALGORITHM = "DESede/ECB/PKCS5Padding";

    /**
     * 生成密钥
     *
     * @return 密钥
     * @throws Exception 异常
     */
    public static String generateKey() throws Exception {
        //实例化密钥生成器
        KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
        //DESede 要求密钥长度为 112位或168位
        kg.init(168);
        //生成密钥
        SecretKey secretKey = kg.generateKey();
        //获得密钥的字符串形式
        return Base64.encodeBase64String(secretKey.getEncoded());
    }

//    /**
//     * des加密
//     *
//     * @param source 源字符串
//     * @param key    密钥
//     * @return 加密后的密文
//     * @throws Exception 异常
//     */
//    public static String encrypt(String source, String key, String iv) throws Exception {
//        byte[] sourceBytes = source.getBytes(StandardCharsets.UTF_8);
//        byte[] keyBytes = Base64.decodeBase64(key);
//        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
//        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
//        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyBytes, KEY_ALGORITHM), ivParameterSpec);
//        byte[] decrypted = cipher.doFinal(sourceBytes);
//        return Base64.encodeBase64String(decrypted);
//    }
//
//    /**
//     * des解密
//     *
//     * @param encryptStr 密文
//     * @param key        密钥
//     * @return 源字符串
//     * @throws Exception 异常
//     */
//    public static String decrypt(String encryptStr, String key, String iv) throws Exception {
//        byte[] sourceBytes = Base64.decodeBase64(encryptStr);
//        byte[] keyBytes = Base64.decodeBase64(key);
//        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
//        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
//        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, KEY_ALGORITHM), ivParameterSpec);
//        byte[] decoded = cipher.doFinal(sourceBytes);
//        return new String(decoded, StandardCharsets.UTF_8);
//    }

    /**
     * 加密方法
     * @param source 源字符串
     * @param keyStr 密钥字符串
     * @return 密文字符串
     */
    public static String encrypt(String source, String keyStr) {
        String encryptStr = null;
        try {
            byte[] sourceBytes = source.getBytes(StandardCharsets.UTF_8);
            SecretKey deskey = new SecretKeySpec(buildDESedeKey(keyStr), KEY_ALGORITHM);  //生成密钥
            Cipher c1 = Cipher.getInstance(KEY_ALGORITHM);
            c1.init(Cipher.ENCRYPT_MODE, deskey);  //初始化为加密模式
            byte[] encrypt = c1.doFinal(sourceBytes);
            encryptStr = Hex.encodeHexString(encrypt);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return encryptStr;
    }

    /**
     * 解密方法
     * @param encryptStr 密文
     * @param keyStr 密钥字符串
     * @return 明文字符串
     */
    public static String decrypt(String encryptStr, String keyStr) {
        try {
            byte[] encrypt = Hex.decodeHex(encryptStr);
            SecretKey deskey = new SecretKeySpec(buildDESedeKey(keyStr), KEY_ALGORITHM);  //生成密钥
            Cipher c1 = Cipher.getInstance(KEY_ALGORITHM);
            c1.init(Cipher.DECRYPT_MODE, deskey);    //初始化为解密模式
            byte[] decrypt = c1.doFinal(encrypt);
            return new String(decrypt);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return null;
    }


    /**
     * 根据字符串生成密钥字节数组
     * @param keyStr 密钥字符串
     * @return 密钥字节数组
     */
    public static byte[] buildDESedeKey(String keyStr) {
        byte[] key = new byte[24];    //声明一个24位的字节数组，默认里面都是0
        byte[] temp = keyStr.getBytes(StandardCharsets.UTF_8);    //将字符串转成字节数组

        //执行数组拷贝
        if(key.length > temp.length){
            //如果temp不够24位，则拷贝temp数组整个长度的内容到key数组中
            System.arraycopy(temp, 0, key, 0, temp.length);
        }else{
            //如果temp大于24位，则拷贝temp数组24个长度的内容到key数组中
            System.arraycopy(temp, 0, key, 0, key.length);
        }
        return key;
    }
}
